package spitter.repository;

import org.springframework.stereotype.Repository;
import spitter.domain.Spitter;
import spitter.dto.SpitterCondition;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

public class SpitterRepositoryImpl implements SpitterCustom {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Spitter> findByCondition(SpitterCondition condition) {
        String jql = "from Spitter s where 1=1 ";
        if (condition.getEmail() != null && !condition.getEmail().equals("")) {
            jql += "and s.email like %" + condition.getEmail() + "%";
        }
        if (condition.getFirstname() != null && !condition.getFirstname().equals("")) {
            jql += "and s.firstname like %" + condition.getFirstname() + "%";
        }
        if (condition.getLastname() != null && !condition.getLastname().equals("")) {
            jql += "and s.lastname like %" + condition.getLastname() + "%";
        }
        TypedQuery<Spitter> query = em.createQuery(jql, Spitter.class);
        return query.getResultList();
    }
}
